#_*_coding:utf-8_*_
__author__ = 'kom'

from scrapy.spider import BaseSpider
from scrapy.selector import HtmlXPathSelector
from finspider.items import FinspiderItem
from scrapy import log
from libs import parse_jsjson

class FinSpider(BaseSpider):
    name = "finspider"
    allowed_domains = []
    start_urls = ["http://www.cmbchina.com/cfweb/svrajax/product.ashx?op=search&type=s&pageindex=1&pagesize=15&salestatus=&baoben=&currency=&term=&keyword=&series=01&risk=&city=&date=&orderby=",]

    def __init__(self):
        super(finspider, self).__init__()
        self.cmbLastest="2010-8-7"
        self.cmbDetailPre="http://www.cmbchina.com/cfweb/personal/productdetail.aspx?code="

    def parse(self, response):
        bank_domains = response.url.split(".")[1]
        parse_fun = getattr(self,bank_domains,"default")
        parse_fun(response)
        #self.cmbchina(response)



    def cmbchina(self,response):
        log.msg("cmbchina kom", level=log.WARNING)
        items=[]
        hxs = parse_jsjson(response.body[1:-1])
        products = hxs["list"]
        for p in products:
            if p["BeginDate"] > self.cmbLastest:
                item=FinspiderItem()
                item["bank"] = "cmbchina"
                item["prdname"] = p["PrdName"]
                item["begindate"] = p["BeginDate"]
                item["enddate"] = p["EndDate"]
                item["expiredate"] = p["ExpireDate"]
                item["type"] = p["TypeCode"]
                item["source"] = self.cmbDetailPre+p["PrdCode"]
                item["start_point"]=self.make_requests_from_url(item["source"]+"&type=prodinfo").replace(callback = self.cmbchina_product)
                items.append(item)
                log.msg(item["start_point"],level=log.WARNING)
        log.msg("kom end", level=log.WARNING)
        return items

    def cmbchina_product(self,response):
        hxs = HtmlXPathSelector(response)
        return hxs.select("//table[@class='buyinfo']/tr[3]/td[4]/text()").extract()[0].strip()




